package com.controller;


import com.dao.ManagerDao;
import com.pojo.Area;
import com.pojo.Manager;
import com.pojo.Operator;
import com.pojo.Poor;
import com.service.Impl.ManagerServiceImpl;
import com.service.ManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/manages")
public class ManagerController {

    @Autowired
    ManageService Ms=new ManagerServiceImpl();



    //查询所有地区信息
       @GetMapping("/area")
       public Result selectAllArea(){
           List<Area> areas = Ms.selectAllArea();

           Integer code=!areas.isEmpty() ? Code.select_Ok:Code.select_ERR;

           String message=!areas.isEmpty() ? "":"查询失败,没有找到相关结果";

           return new Result(code,areas,message);
       }
       //添加地区信息
       @PostMapping("/area")
       public Result addArea(@RequestBody Area area){
           boolean flag = Ms.addArea(area);
           return  new Result(flag ? Code.add_Ok:Code.add_ERR,flag);
       }

       //修改地区信息
       @PutMapping("/area")
       public Result updateArea(@RequestBody Area area) {
           boolean flag = Ms.updateArea(area);
           return  new Result(flag ? Code.update_Ok:Code.update_ERR,flag);
       }

       //删除
    @DeleteMapping("/area/{id}")
    public Result deleteArea(@PathVariable Integer id) {
        boolean flag = Ms.deleteArea(id);
        return  new Result(flag ? Code.delete_Ok:Code.delete_ERR,flag);
    }

    //查询所有贫困种类信息
    @GetMapping("/poor")
    public Result selectAllPoor(){

        List<Poor> poors = Ms.selectAllPoor();
        Integer code=!poors.isEmpty() ? Code.select_Ok:Code.select_ERR;

        String message=!poors.isEmpty() ? "":"查询失败,没有找到相关结果";

        return new Result(code,poors,message);
    }
    //添加贫困种类信息
    @PostMapping("/poor")
    public Result addPoor(@RequestBody Poor poor){
        boolean flag = Ms.addPoor(poor);
        return  new Result(flag ? Code.add_Ok:Code.add_ERR,flag);
    }

    //修改贫困种类信息
    @PutMapping("/poor")
    public Result updatePoor(@RequestBody Poor poor) {
        boolean flag = Ms.updatePoor(poor);
        return  new Result(flag ? Code.update_Ok:Code.update_ERR,flag);
    }

    //删除
    @DeleteMapping("/poor/{id}")
    public Result deletePoor(@PathVariable Integer id) {
        boolean flag = Ms.deletePoor(id);
        return  new Result(flag ? Code.delete_Ok:Code.delete_ERR,flag);
    }


    //查询某地区所有操作员信息
    @GetMapping("/operator/{id}")
    public Result selectAllOperator(@PathVariable Integer id){
        List<Operator> operators = Ms.selectAllOperator(id);

        Integer code=!operators.isEmpty() ? Code.select_Ok:Code.select_ERR;

        String message=!operators.isEmpty() ? "":"查询失败,没有找到相关结果";

        return new Result(code,operators,message);

    }

    //添加操作员信息
    @PostMapping("/operator")
    public Result addOperator(@RequestBody Operator operator){
        boolean flag = Ms.addOperator(operator);
        return  new Result(flag ? Code.add_Ok:Code.add_ERR,flag);
    }

    //更新操作员信息
    @PutMapping("/operator")
    public Result updateOperator(@RequestBody Operator operator){
        boolean flag = Ms.updateOperator(operator);
        return  new Result(flag ? Code.update_Ok:Code.update_ERR,flag);
    }

    //删除操作员信息
    @DeleteMapping("/operator/{id}")
    public Result deleteOperator(@PathVariable Integer id){
        boolean flag = Ms.deleteOperator(id);
        return  new Result(flag ? Code.delete_Ok:Code.delete_ERR,flag);
    }


    //管理员登入
    @PostMapping("/login")
    public Result login(@RequestBody Manager manager){
           log.info("{}",manager);
        boolean flag = Ms.login(manager);
        return  new Result(flag ? Code.select_Ok:Code.select_ERR,flag);
    }
}
